parameters:
  buildConfig: ''
  archType: ''
  osGroup: ''
  osSubgroup: ''
  container: ''
  helixQueues: ''
  runtimeVariant: ''
  crossBuild: false
  variables: {}
  pool: ''

  # When set to a non-empty value (Debug / Release), it determines libraries
  # build configuration to use for the tests. Setting this property implies
  # a dependency of this job on the appropriate libraries build and is used
  # to construct the name of the Azure artifact representing libraries build
  # to use for building the tests.
  liveLibrariesBuildConfig: ''

### Crossgen-comparison build job
###
### Ensure that the output of cross-architecture, e.g. x64-hosted-arm-targeting,
### crossgen matches that of native, e.g. arm-hosted-arm-targeting, crossgen.

jobs:
- template: /eng/pipelines/common/templates/runtimes/xplat-job.yml
  parameters:
    buildConfig: ${{ parameters.buildConfig }}
    archType: ${{ parameters.archType }}
    osGroup: ${{ parameters.osGroup }}
    osSubgroup: ${{ parameters.osSubgroup }}
    runtimeVariant: ${{ parameters.runtimeVariant }}
    liveLibrariesBuildConfig: ${{ parameters.liveLibrariesBuildConfig }}
    helixType: 'test/crossgen-comparison/'
    pool: ${{ parameters.pool }}

    # Compute job name from template parameters
    name: ${{ format('test_crossgen2_comparison_build_{0}{1}_{2}_{3}', parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig) }}
    displayName: ${{ format('Test crossgen2-comparison build {0}{1} {2} {3}', parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig) }}

    crossBuild: ${{ parameters.crossBuild }}

    variables:
    - ${{ if eq(variables['System.TeamProject'], 'internal') }}:
      - group: DotNet-HelixApi-Access
    - name: hostArchType
      value: x64
    - name: targetFlavor
      value: $(osGroup).$(archType).$(buildConfigUpper)
    - name: crossFlavor
      value: $(osGroup).$(hostArchType)_$(archType).$(buildConfigUpper)
    - name: artifactsDirectory
      value: $(Build.SourcesDirectory)$(dir)artifacts
    - name: binDirectory
      value: $(artifactsDirectory)$(dir)bin
    - name: productDirectory
      value: $(binDirectory)$(dir)coreclr
    - name: workItemDirectory
      value: $(artifactsDirectory)$(dir)tests$(dir)coreclr$(dir)$(targetFlavor)$(dir)Tests$(dir)CrossCompileRoot
    - name: crossgencompare_build_artifact
      value: crossgen_comparison_build_$(osGroup)$(osSubgroup)_$(archType)
    - ${{ if eq(parameters.osGroup, 'windows') }}:
      - name: target_crossgen2_os
        value: windows
    - ${{ if eq(parameters.osGroup, 'linux') }}:
      - name: target_crossgen2_os
        value: linux
    - ${{ if eq(parameters.osGroup, 'osx') }}:
      - name: target_crossgen2_os
        value: osx
    - name: crossgen2location
      value: $(productDirectory)$(dir)$(targetFlavor)$(dir)crossgen2$(dir)crossgen2.dll
    - ${{ if ne(parameters.archType, 'x64') }}:
      - name: crossgen2location
        value: $(productDirectory)$(dir)$(targetFlavor)$(dir)x64$(dir)crossgen2$(dir)tools$(dir)crossgen2.dll
    - name: librariesProductDllDir
      value: $(Build.SourcesDirectory)$(dir)artifacts$(dir)bin$(dir)runtime$(dir)net9.0-$(osGroup)$(osSubgroup)-$(buildConfig)-$(archType)

    - ${{ parameters.variables }}

    # Test job depends on the corresponding build job
    dependsOn:
    - build_${{ parameters.osGroup }}${{ parameters.osSubgroup }}_${{ parameters.archType }}_${{ parameters.buildConfig }}_

    # Run all steps in the container.
    # Note that the containers are defined in platform-matrix.yml
    container: ${{ parameters.container }}
    timeoutInMinutes: 180 # 3 hrs

    steps:

    # Download product build
    - template: /eng/pipelines/common/download-artifact-step.yml
      parameters:
        unpackFolder: $(Build.SourcesDirectory)/artifacts/bin
        artifactFileName: 'Release_CoreCLR_$(osGroup)$(osSubgroup)_$(archType)_$(_BuildConfig)$(archiveExtension)'
        artifactName: 'Release_CoreCLR_$(osGroup)$(osSubgroup)_$(archType)_$(_BuildConfig)'
        displayName: 'product build'

    # Populate Core_Root
    - script: $(Build.SourcesDirectory)/src/tests/build$(scriptExt) $(buildConfig) $(archType) $(crossArg) generatelayoutonly
      displayName: Populate Core_Root

    # Create work item directory and populate with assemblies
    - ${{ if ne(parameters.osGroup, 'windows') }}:
      - script: |
          mkdir -p $(workItemDirectory)
          mkdir -p $(workItemDirectory)/log
          mkdir -p $(workItemDirectory)/dlls
          cp $(librariesProductDllDir)/* $(workItemDirectory)/dlls
          cp $(productDirectory)/$(targetFlavor)/IL/System.Private.CoreLib.dll $(workItemDirectory)/dlls
        displayName: Create directories
        failOnStderr: true
    - ${{ if eq(parameters.osGroup, 'windows') }}:
      - script: |
          md $(workItemDirectory)\log
          md $(workItemDirectory)\dlls
          echo copy $(librariesProductDllDir)\* $(workItemDirectory)\dlls
          copy $(librariesProductDllDir)\* $(workItemDirectory)\dlls
          echo copy $(productDirectory)\$(targetFlavor)\IL\System.Private.CoreLib.dll $(workItemDirectory)\dlls
          copy $(productDirectory)\$(targetFlavor)\IL\System.Private.CoreLib.dll $(workItemDirectory)\dlls
        displayName: Create directories
        failOnStderr: true

    # Create baseline output on the host (x64) machine
    - task: PythonScript@0
      displayName: Create cross-platform crossgen baseline
      inputs:
        scriptSource: 'filePath'
        scriptPath: $(Build.SourcesDirectory)/src/tests/Common/scripts/crossgen2_comparison.py
        ${{ if ne(parameters.osGroup, 'windows') }}:
          pythonInterpreter: /usr/bin/python3
          arguments:
            crossgen_framework
            --crossgen    $(crossgen2location)
            --dotnet      $(Build.SourcesDirectory)/dotnet.sh
            --core_root   $(workItemDirectory)/dlls
            --result_dir  $(workItemDirectory)/log
            --target_os   $(target_crossgen2_os)
            --target_arch $(archType)
            --compiler_arch_os $(target_crossgen2_os)_x64
        ${{ if eq(parameters.osGroup, 'windows') }}:
          arguments:
            crossgen_framework
            --crossgen    $(crossgen2location)
            --dotnet      $(Build.SourcesDirectory)\dotnet.cmd
            --core_root   $(workItemDirectory)\dlls
            --result_dir  $(workItemDirectory)\log
            --target_os   $(target_crossgen2_os)
            --target_arch $(archType)
            --compiler_arch_os $(target_crossgen2_os)_x64

    - task: PublishPipelineArtifact@1
      displayName: Publish cross compiled component
      inputs:
        targetPath: $(workItemDirectory)
        artifactName: $(crossgencompare_build_artifact)
      continueOnError: true
      condition: always()
